using UnityEngine;
using System.Collections.Generic;

/// <summary>
/// It's attached on a GameObject to be a mirror of the 'gameObjectWall'.
/// </summary>
public class WallMirror : BaseBehaviour
{
    public Wall WallToMirror;
    private IList<GameObjectTransform> blocks;
    private IList<GameObjectTransform> blocksToMirror;

	protected override void start()
	{
        storeBlocksToMirror();
		createBlocksContainer();
		storeBlocksInContainer();
		disableBlocks();        

		base.start();
	}

    private void storeBlocksToMirror()
    {
        Wall wallToMirror = WallToMirror.GetComponent<Wall>();
        blocksToMirror = new List<GameObjectTransform>();
        blocksToMirror = wallToMirror.getBlocks();
    }

	private void createBlocksContainer()
	{
        blocks = new List<GameObjectTransform>();
	}

	private void storeBlocksInContainer()
	{
		for (int i = 0; i < transform.childCount; i++)
		{
            WallBlock wallBlock = transform.GetChild(i).GetComponent<WallBlock>();
            blocks.Add(wallBlock);
		}
	}

    private void disableBlocks()
	{
        WallBlock[] wallBlocks = gameObject.GetComponentsInChildren<WallBlock>();
       
        foreach (WallBlock wallBlock in wallBlocks)
            wallBlock.disableBehaviour();
	}

	protected override void update(float deltaTime)
	{
        updateWallBlockPositions();
	}

    private void updateWallBlockPositions()
    {
        for (int i = 0; i < blocks.Count; i++)
            blocks[i].setPosition(blocksToMirror[i].getPosition());
    }
}